<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <base data-ice="baseUrl" href="../../../">
  <title data-ice="title">src/toolbar/ThreeDMode.js | xeokit-bim-viewer</title>
  <link type="text/css" rel="stylesheet" href="css/style.css">
  <link type="text/css" rel="stylesheet" href="css/prettify-tomorrow.css">
  <script src="script/prettify/prettify.js"></script>
  <script src="script/manual.js"></script>
<meta name="description" content="BIM viewer built on xeokit"><meta property="og:type" content="website"><meta property="og:url" content="https://github.com/xeokit/xeokit-bim-viewer"><meta property="og:site_name" content="xeokit-bim-viewer"><meta property="og:title" content="xeokit-bim-viewer"><meta property="og:image" content="./images/logo.jpg"><meta property="og:description" content="BIM viewer built on xeokit"><meta property="og:author" content="http://xeolabs.com"><meta property="twitter:card" content="summary"><meta property="twitter:title" content="xeokit-bim-viewer"><meta property="twitter:description" content="BIM viewer built on xeokit"><meta property="twitter:image" content="./images/logo.jpg"></head>
<body class="layout-container" data-ice="rootContainer">

<header>
  <a href="./" style="display: flex; align-items: center;"><img src="./image/brand_logo.jpg" style="width:34px;"></a>
  
  <a href="identifiers.html">Reference</a>
  <a href="source.html">Source</a>
  
  <div class="search-box">
  <span>
    <img src="./image/search.png">
    <span class="search-input-edge"></span><input class="search-input"><span class="search-input-edge"></span>
  </span>
    <ul class="search-result"></ul>
  </div>
<a style="position:relative; top:3px;" href="https://github.com/xeokit/xeokit-bim-viewer"><img width="20px" src="./image/github.png"></a></header>

<nav class="navigation" data-ice="nav"><div>
  <ul>
    
  <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/BIMViewer.js~BIMViewer.html">BIMViewer</a></span></span></li>
<li data-ice="doc"><a data-ice="dirPath" class="nav-dir-path" href="identifiers.html#ifcelementdescriptions">IFCElementDescriptions</a><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-IFCElementDescriptions">IFCElementDescriptions</a></span></span></li>
<li data-ice="doc"><a data-ice="dirPath" class="nav-dir-path" href="identifiers.html#contextmenus">contextMenus</a><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/contextMenus/SectionToolContextMenu.js~SectionToolContextMenu.html">SectionToolContextMenu</a></span></span></li>
<li data-ice="doc"><a data-ice="dirPath" class="nav-dir-path" href="identifiers.html#server">server</a><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/server/Server.js~Server.html">Server</a></span></span></li>
</ul>
</div>
</nav>

<div class="content" data-ice="content"><h1 data-ice="title">src/toolbar/ThreeDMode.js</h1>
<pre class="source-code line-number raw-source-code"><code class="prettyprint linenums" data-ice="content">import {Controller} from &quot;../Controller.js&quot;;
import {math} from &quot;@xeokit/xeokit-sdk/dist/xeokit-sdk.es.js&quot;;

const tempVec3a = math.vec3();

/** @private */
class ThreeDMode extends Controller {

    constructor(parent, cfg) {

        super(parent, cfg);

        if (!cfg.buttonElement) {
            throw &quot;Missing config: buttonElement&quot;;
        }

        this._saveOrthoActive = null;
        this._buttonElement = cfg.buttonElement;

        this._cameraControlNavModeMediator = cfg.cameraControlNavModeMediator;

        this._active = false;

        this.on(&quot;enabled&quot;, (enabled) =&gt; {
            if (!enabled) {
                this._buttonElement.classList.add(&quot;disabled&quot;);
            } else {
                this._buttonElement.classList.remove(&quot;disabled&quot;);
            }
        });

        this._buttonElement.addEventListener(&quot;click&quot;, (event) =&gt; {
            if (this.getEnabled()) {
                this.bimViewer._sectionTool.hideControl();
                this.setActive(!this.getActive(), () =&gt; { // Animated
                });
            }
            event.preventDefault();
        });

        this.bimViewer.on(&quot;reset&quot;, () =&gt; {
            this.setActive(true, () =&gt; { // Animated
            });
        });
    }

    setEnabled(enabled) {
        super.setEnabled(enabled);
        this._saveOrthoActive = this.bimViewer._orthoMode.getActive();
    }

    setActive(active, done) {
        if (this._active === active) {
            if (done) {
                done();
            }
            return;
        }
        this._active = active;
        if (active) {
            this._buttonElement.classList.add(&quot;active&quot;);
            if (done) {
                this._enterThreeDMode(() =&gt; {
                    this.fire(&quot;active&quot;, this._active);
                    done();
                });
            } else {
                this._enterThreeDMode();
                this.fire(&quot;active&quot;, this._active);
            }
        } else {
            this._buttonElement.classList.remove(&quot;active&quot;);
            if (done) {
                this._exitThreeDMode(() =&gt; {
                    this.fire(&quot;active&quot;, this._active);
                    done();
                });
            } else {
                this._exitThreeDMode();
                this.fire(&quot;active&quot;, this._active);
            }
        }
    }

    _enterThreeDMode(done) {

        const viewer = this.viewer;
        const scene = viewer.scene;
        const aabb = scene.getAABB(scene.visibleObjectIds);
        const diag = math.getAABB3Diag(aabb);
        const center = math.getAABB3Center(aabb, tempVec3a);
        const dist = Math.abs(diag / Math.tan(65.0 / 2));     // TODO: fovy match with CameraFlight
        const camera = scene.camera;
        const dir = (camera.yUp) ? [-1, -1, -1] : [1, 1, 1];
        const up = (camera.yUp) ? [-1, 1, -1] : [-1, 1, 1];

        viewer.cameraControl.pivotPos = center;

        this.bimViewer._navCubeMode.setActive(true);
        this.bimViewer._firstPersonMode.setEnabled(true);
        this._cameraControlNavModeMediator.setThreeDModeActive(true);
        this.bimViewer._sectionTool.setEnabled(true);
        this.bimViewer._orthoMode.setEnabled(true);

        if (done) {
            viewer.cameraFlight.flyTo({
                look: center,
                eye: [center[0] - (dist * dir[0]), center[1] - (dist * dir[1]), center[2] - (dist * dir[2])],
                up: up,
                orthoScale: diag * 1.3,
                duration: 1,
                projection: this._saveOrthoActive ? &quot;ortho&quot; : &quot;perspective&quot;
            }, () =&gt; {
                done();
            });
        } else {
            viewer.cameraFlight.jumpTo({
                look: center,
                eye: [center[0] - (dist * dir[0]), center[1] - (dist * dir[1]), center[2] - (dist * dir[2])],
                up: up,
                orthoScale: diag * 1.3,
                projection: this._saveOrthoActive ? &quot;ortho&quot; : &quot;perspective&quot;
            });
        }
    }

    _exitThreeDMode(done) {

        const viewer = this.viewer;
        const scene = viewer.scene;
        const camera = scene.camera;
        const aabb = scene.getAABB(scene.visibleObjectIds);
        const look2 = math.getAABB3Center(aabb);
        const diag = math.getAABB3Diag(aabb);
        const fitFOV = 45; // fitFOV;
        const sca = Math.abs(diag / Math.tan(fitFOV * math.DEGTORAD));
        const orthoScale2 = diag * 1.3;
        const eye2 = tempVec3a;

        eye2[0] = look2[0] + (camera.worldUp[0] * sca);
        eye2[1] = look2[1] + (camera.worldUp[1] * sca);
        eye2[2] = look2[2] + (camera.worldUp[2] * sca);

        const up2 = math.mulVec3Scalar(camera.worldForward, -1, []);

        this.bimViewer._sectionTool.setActive(false);
        this.bimViewer._firstPersonMode.setEnabled(false);

        this._saveOrthoActive = this.bimViewer._orthoMode.getActive();
         this.bimViewer._orthoMode.setEnabled(false);

        this._cameraControlNavModeMediator.setThreeDModeActive(false);

        if (done) {
            viewer.cameraFlight.flyTo({
                eye: eye2,
                look: look2,
                up: up2,
                orthoScale: orthoScale2,
                projection: &quot;ortho&quot;
            }, () =&gt; {
                this.bimViewer._navCubeMode.setActive(false);
            });
        } else {
            viewer.cameraFlight.jumpTo({
                eye: eye2,
                look: look2,
                up: up2,
                orthoScale: orthoScale2,
                projection: &quot;ortho&quot;
            });
            this.bimViewer._navCubeMode.setActive(false);
        }
    }
}

export {ThreeDMode};</code></pre>

</div>

<footer class="footer">
  Generated by <a href="https://esdoc.org">ESDoc<span data-ice="esdocVersion">(1.1.0)</span><img src="./image/esdoc-logo-mini-black.png"></a>
</footer>

<script src="script/search_index.js"></script>
<script src="script/search.js"></script>
<script src="script/pretty-print.js"></script>
<script src="script/inherited-summary.js"></script>
<script src="script/test-summary.js"></script>
<script src="script/inner-link.js"></script>
<script src="script/patch-for-local.js"></script>
</body>
</html>
